package com.bawei.exam57.fragment

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.exam57.R
import com.bawei.exam57.adpater.GoodAdpater
import com.bawei.exam57.base.BaseFragment
import com.bawei.exam57.databinding.FragmentBlankBinding
import com.bawei.exam57.room.JfEntity
import com.bawei.exam57.room.RoomUtils
import com.bawei.exam57.viewmodel.HomeViewModel
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.youth.banner.loader.ImageLoader
import java.text.SimpleDateFormat


class BlankFragment : BaseFragment<FragmentBlankBinding,HomeViewModel>() {
    val imgs= mutableListOf<String>()
    val sdf= SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    lateinit var goodAdpater: GoodAdpater
    override fun initView() {
        goodAdpater= GoodAdpater()
        binding.rvGood.adapter=goodAdpater
        viewModel.getHome()
        viewModel.getGood(10,1)
        binding.banner.setOnBannerListener {
            ToastUtils.showShort("点击了${it}")
        }
        binding.tv1.setOnClickListener{
            ARouter.getInstance().build("/app/JFActivity").navigation()
        }
        goodAdpater.setOnItemClickListener{ _, _, position ->
//            弹框
            AlertDialog.Builder(requireContext())
                .setMessage("是否兑换")
                .setPositiveButton("确定") { _, _ ->
                    val goodItem = goodAdpater.data[position]
                    if (SPUtils.getInstance().getInt("jf") >= goodItem.ficti) {
                        //积分够
                        val millis = System.currentTimeMillis()
                        val entity = JfEntity(sdf.format(millis), true, goodItem.ficti)
                        RoomUtils.getJFDb().jfDao().insert(entity)
                        SPUtils.getInstance()
                            .put("jf", SPUtils.getInstance().getInt("jf") - goodItem.ficti)
                        ToastUtils.showLong("兑换成功")
                    }else{
                        ToastUtils.showLong("积分不足")
                    }
                }
                .setNegativeButton("取消") { _, _ ->}
                .show()
        }
    }

    override fun initData() {
        viewModel.home.observe(this){

            it.data.banner.forEach {
                imgs.add(it.pic)
            }
            binding.banner.setImages(imgs)
                .setImageLoader(object : ImageLoader() {
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        Glide.with(context!!).load(path).into(imageView!!)
                    }

                }).start()
        }
        viewModel.good.observe(this){
            goodAdpater.data.clear()
            goodAdpater.data.addAll(it.data.list)
            goodAdpater.notifyDataSetChanged()
        }
    }

    override fun getViewModelClass(): Class<HomeViewModel> {
        return HomeViewModel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_blank
    }

}